package com.example.demo.service.impl;

import com.example.demo.dto.TaskDTO;
import com.example.demo.dto.VersionStatisticsDTO;
import com.example.demo.dto.DepStatisticsDTO;
import com.example.demo.dto.AppStatisticsDTO;
import com.example.demo.entity.Task;
import com.example.demo.exception.BusinessException;
import com.example.demo.mapper.TaskMapper;
import com.example.demo.request.CreateTaskRequest;
import com.example.demo.service.TaskService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskMapper taskMapper;

    @Override
    public List<TaskDTO> getTasks(int page, int size, String status, String application, String department) {
        int offset = page * size;
        List<Task> tasks = taskMapper.selectPage(offset, size, status, application, department);
        return tasks.stream().map(t -> {
            String realName = taskMapper.selectRealNameByUserName(t.getCreatedBy());
            return TaskDTO.fromEntity(t, realName);
        }).collect(Collectors.toList());
    }

    @Override
    public long countTasks(String status, String application, String department) {
        return taskMapper.count(status, application, department);
    }

    @Override
    public TaskDTO getTaskById(Long id) {
        Task task = taskMapper.selectById(id);
        if (task == null) {
            throw new BusinessException("任务不存在，ID：" + id);
        }
        String realName = taskMapper.selectRealNameByUserName(task.getCreatedBy());
        return TaskDTO.fromEntity(task, realName);
    }

    @Override
    @Transactional
    public Task createTask(CreateTaskRequest request) {
        Task task = new Task();
        task.setTitle(request.getTitle());
        task.setTaskType(request.getTaskType());
        task.setDepartment(request.getDepartment());
        task.setApplication(request.getApplication());
        task.setRepository(request.getRepository());
        task.setDescription(request.getDescription());
        task.setCreatedBy(request.getCreatedBy());

        task.setStatus("pending");
        task.setCreateTime(LocalDateTime.now());
        task.setUpdateTime(LocalDateTime.now());

        int result = taskMapper.insert(task);
        if (result == 0) {
            throw new BusinessException("任务创建失败");
        }
        return task;
    }

    @Override
    @Transactional
    public void updateTask(Long id, CreateTaskRequest request) {
        Task existing = taskMapper.selectById(id);
        if (existing == null) {
            throw new BusinessException("任务不存在，无法更新");
        }

        existing.setTitle(request.getTitle());
        existing.setTaskType(request.getTaskType());
        existing.setApplication(request.getApplication());
        existing.setRepository(request.getRepository());
        existing.setDescription(request.getDescription());
        existing.setUpdateTime(LocalDateTime.now());

        int result = taskMapper.update(existing);
        if (result == 0) {
            throw new BusinessException("任务更新失败");
        }
    }

    @Override
    @Transactional
    public void deleteTask(Long id) {
        Task task = taskMapper.selectById(id);
        if (task == null) {
            throw new BusinessException("任务不存在，无法删除");
        }
        int result = taskMapper.deleteById(id);
        if (result == 0) {
            throw new BusinessException("任务删除失败");
        }
    }
    
    @Override
    public List<VersionStatisticsDTO> getVersionStatistics() {
        return taskMapper.selectVersionStatistics();
    }
    
    @Override
    public List<DepStatisticsDTO> getDepStatistics(String version) {
        return taskMapper.selectDepStatistics(version);
    }
    
    @Override
    public List<AppStatisticsDTO> getAppStatistics(String version, String department) {
        return taskMapper.selectAppStatistics(version, department);
    }
}